{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example: Card Deck"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's go back to a previous example we worked with, our card deck.\n",
    "\n",
    "Let's rebuild it quickly, but this time using generators instead of a custom iterator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from collections import namedtuple\n",
    "\n",
    "Card = namedtuple('Card', 'rank, suit')\n",
    "SUITS = ('Spades', 'Hearts', 'Diamonds', 'Clubs')\n",
    "RANKS = tuple(range(2, 11)) + tuple('JQKA')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Recall how we recovered the suit and rank for any given card index in the deck (assuming the sorting is `2S...AS  2H...AH  2D...AD  2C...AC`):\n",
    "\n",
    "```\n",
    "suit_index = card_index // len(RANKS)\n",
    "rank_index = card_index % len(RANKS)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def card_gen():\n",
    "    for i in range(len(SUITS) * len(RANKS)):\n",
    "        suit = SUITS[i // len(RANKS)]\n",
    "        rank = RANKS[i % len(RANKS)]\n",
    "        card = Card(rank, suit)\n",
    "        yield card"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So now we can iterate using our generator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Card(rank=2, suit='Spades')\n",
      "Card(rank=3, suit='Spades')\n",
      "Card(rank=4, suit='Spades')\n",
      "Card(rank=5, suit='Spades')\n",
      "Card(rank=6, suit='Spades')\n",
      "Card(rank=7, suit='Spades')\n",
      "Card(rank=8, suit='Spades')\n",
      "Card(rank=9, suit='Spades')\n",
      "Card(rank=10, suit='Spades')\n",
      "Card(rank='J', suit='Spades')\n",
      "Card(rank='Q', suit='Spades')\n",
      "Card(rank='K', suit='Spades')\n",
      "Card(rank='A', suit='Spades')\n",
      "Card(rank=2, suit='Hearts')\n",
      "Card(rank=3, suit='Hearts')\n",
      "Card(rank=4, suit='Hearts')\n",
      "Card(rank=5, suit='Hearts')\n",
      "Card(rank=6, suit='Hearts')\n",
      "Card(rank=7, suit='Hearts')\n",
      "Card(rank=8, suit='Hearts')\n",
      "Card(rank=9, suit='Hearts')\n",
      "Card(rank=10, suit='Hearts')\n",
      "Card(rank='J', suit='Hearts')\n",
      "Card(rank='Q', suit='Hearts')\n",
      "Card(rank='K', suit='Hearts')\n",
      "Card(rank='A', suit='Hearts')\n",
      "Card(rank=2, suit='Diamonds')\n",
      "Card(rank=3, suit='Diamonds')\n",
      "Card(rank=4, suit='Diamonds')\n",
      "Card(rank=5, suit='Diamonds')\n",
      "Card(rank=6, suit='Diamonds')\n",
      "Card(rank=7, suit='Diamonds')\n",
      "Card(rank=8, suit='Diamonds')\n",
      "Card(rank=9, suit='Diamonds')\n",
      "Card(rank=10, suit='Diamonds')\n",
      "Card(rank='J', suit='Diamonds')\n",
      "Card(rank='Q', suit='Diamonds')\n",
      "Card(rank='K', suit='Diamonds')\n",
      "Card(rank='A', suit='Diamonds')\n",
      "Card(rank=2, suit='Clubs')\n",
      "Card(rank=3, suit='Clubs')\n",
      "Card(rank=4, suit='Clubs')\n",
      "Card(rank=5, suit='Clubs')\n",
      "Card(rank=6, suit='Clubs')\n",
      "Card(rank=7, suit='Clubs')\n",
      "Card(rank=8, suit='Clubs')\n",
      "Card(rank=9, suit='Clubs')\n",
      "Card(rank=10, suit='Clubs')\n",
      "Card(rank='J', suit='Clubs')\n",
      "Card(rank='Q', suit='Clubs')\n",
      "Card(rank='K', suit='Clubs')\n",
      "Card(rank='A', suit='Clubs')\n"
     ]
    }
   ],
   "source": [
    "for card in card_gen():\n",
    "    print(card)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But we can really simplify this further!\n",
    "\n",
    "We don't have to use these indices at all!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def card_gen():\n",
    "    for suit in SUITS:\n",
    "        for rank in RANKS:\n",
    "            card = Card(rank, suit)\n",
    "            yield card"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And we now have the same functionality:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Card(rank=2, suit='Spades')\n",
      "Card(rank=3, suit='Spades')\n",
      "Card(rank=4, suit='Spades')\n",
      "Card(rank=5, suit='Spades')\n",
      "Card(rank=6, suit='Spades')\n",
      "Card(rank=7, suit='Spades')\n",
      "Card(rank=8, suit='Spades')\n",
      "Card(rank=9, suit='Spades')\n",
      "Card(rank=10, suit='Spades')\n",
      "Card(rank='J', suit='Spades')\n",
      "Card(rank='Q', suit='Spades')\n",
      "Card(rank='K', suit='Spades')\n",
      "Card(rank='A', suit='Spades')\n",
      "Card(rank=2, suit='Hearts')\n",
      "Card(rank=3, suit='Hearts')\n",
      "Card(rank=4, suit='Hearts')\n",
      "Card(rank=5, suit='Hearts')\n",
      "Card(rank=6, suit='Hearts')\n",
      "Card(rank=7, suit='Hearts')\n",
      "Card(rank=8, suit='Hearts')\n",
      "Card(rank=9, suit='Hearts')\n",
      "Card(rank=10, suit='Hearts')\n",
      "Card(rank='J', suit='Hearts')\n",
      "Card(rank='Q', suit='Hearts')\n",
      "Card(rank='K', suit='Hearts')\n",
      "Card(rank='A', suit='Hearts')\n",
      "Card(rank=2, suit='Diamonds')\n",
      "Card(rank=3, suit='Diamonds')\n",
      "Card(rank=4, suit='Diamonds')\n",
      "Card(rank=5, suit='Diamonds')\n",
      "Card(rank=6, suit='Diamonds')\n",
      "Card(rank=7, suit='Diamonds')\n",
      "Card(rank=8, suit='Diamonds')\n",
      "Card(rank=9, suit='Diamonds')\n",
      "Card(rank=10, suit='Diamonds')\n",
      "Card(rank='J', suit='Diamonds')\n",
      "Card(rank='Q', suit='Diamonds')\n",
      "Card(rank='K', suit='Diamonds')\n",
      "Card(rank='A', suit='Diamonds')\n",
      "Card(rank=2, suit='Clubs')\n",
      "Card(rank=3, suit='Clubs')\n",
      "Card(rank=4, suit='Clubs')\n",
      "Card(rank=5, suit='Clubs')\n",
      "Card(rank=6, suit='Clubs')\n",
      "Card(rank=7, suit='Clubs')\n",
      "Card(rank=8, suit='Clubs')\n",
      "Card(rank=9, suit='Clubs')\n",
      "Card(rank=10, suit='Clubs')\n",
      "Card(rank='J', suit='Clubs')\n",
      "Card(rank='Q', suit='Clubs')\n",
      "Card(rank='K', suit='Clubs')\n",
      "Card(rank='A', suit='Clubs')\n"
     ]
    }
   ],
   "source": [
    "for card in card_gen():\n",
    "    print(card)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now make it into an iterable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class CardDeck:\n",
    "    SUITS = ('Spades', 'Hearts', 'Diamonds', 'Clubs')\n",
    "    RANKS = tuple(range(2, 11)) + tuple('JQKA')\n",
    "        \n",
    "    def __iter__(self):\n",
    "        return CardDeck.card_gen()\n",
    "    \n",
    "    @staticmethod\n",
    "    def card_gen():\n",
    "        for suit in CardDeck.SUITS:\n",
    "            for rank in CardDeck.RANKS:\n",
    "                card = Card(rank, suit)\n",
    "                yield card\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "deck = CardDeck()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Card(rank=2, suit='Spades'),\n",
       " Card(rank=3, suit='Spades'),\n",
       " Card(rank=4, suit='Spades'),\n",
       " Card(rank=5, suit='Spades'),\n",
       " Card(rank=6, suit='Spades'),\n",
       " Card(rank=7, suit='Spades'),\n",
       " Card(rank=8, suit='Spades'),\n",
       " Card(rank=9, suit='Spades'),\n",
       " Card(rank=10, suit='Spades'),\n",
       " Card(rank='J', suit='Spades'),\n",
       " Card(rank='Q', suit='Spades'),\n",
       " Card(rank='K', suit='Spades'),\n",
       " Card(rank='A', suit='Spades'),\n",
       " Card(rank=2, suit='Hearts'),\n",
       " Card(rank=3, suit='Hearts'),\n",
       " Card(rank=4, suit='Hearts'),\n",
       " Card(rank=5, suit='Hearts'),\n",
       " Card(rank=6, suit='Hearts'),\n",
       " Card(rank=7, suit='Hearts'),\n",
       " Card(rank=8, suit='Hearts'),\n",
       " Card(rank=9, suit='Hearts'),\n",
       " Card(rank=10, suit='Hearts'),\n",
       " Card(rank='J', suit='Hearts'),\n",
       " Card(rank='Q', suit='Hearts'),\n",
       " Card(rank='K', suit='Hearts'),\n",
       " Card(rank='A', suit='Hearts'),\n",
       " Card(rank=2, suit='Diamonds'),\n",
       " Card(rank=3, suit='Diamonds'),\n",
       " Card(rank=4, suit='Diamonds'),\n",
       " Card(rank=5, suit='Diamonds'),\n",
       " Card(rank=6, suit='Diamonds'),\n",
       " Card(rank=7, suit='Diamonds'),\n",
       " Card(rank=8, suit='Diamonds'),\n",
       " Card(rank=9, suit='Diamonds'),\n",
       " Card(rank=10, suit='Diamonds'),\n",
       " Card(rank='J', suit='Diamonds'),\n",
       " Card(rank='Q', suit='Diamonds'),\n",
       " Card(rank='K', suit='Diamonds'),\n",
       " Card(rank='A', suit='Diamonds'),\n",
       " Card(rank=2, suit='Clubs'),\n",
       " Card(rank=3, suit='Clubs'),\n",
       " Card(rank=4, suit='Clubs'),\n",
       " Card(rank=5, suit='Clubs'),\n",
       " Card(rank=6, suit='Clubs'),\n",
       " Card(rank=7, suit='Clubs'),\n",
       " Card(rank=8, suit='Clubs'),\n",
       " Card(rank=9, suit='Clubs'),\n",
       " Card(rank=10, suit='Clubs'),\n",
       " Card(rank='J', suit='Clubs'),\n",
       " Card(rank='Q', suit='Clubs'),\n",
       " Card(rank='K', suit='Clubs'),\n",
       " Card(rank='A', suit='Clubs')]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[card for card in deck]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And of course we can do it again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Card(rank=2, suit='Spades'),\n",
       " Card(rank=3, suit='Spades'),\n",
       " Card(rank=4, suit='Spades'),\n",
       " Card(rank=5, suit='Spades'),\n",
       " Card(rank=6, suit='Spades'),\n",
       " Card(rank=7, suit='Spades'),\n",
       " Card(rank=8, suit='Spades'),\n",
       " Card(rank=9, suit='Spades'),\n",
       " Card(rank=10, suit='Spades'),\n",
       " Card(rank='J', suit='Spades'),\n",
       " Card(rank='Q', suit='Spades'),\n",
       " Card(rank='K', suit='Spades'),\n",
       " Card(rank='A', suit='Spades'),\n",
       " Card(rank=2, suit='Hearts'),\n",
       " Card(rank=3, suit='Hearts'),\n",
       " Card(rank=4, suit='Hearts'),\n",
       " Card(rank=5, suit='Hearts'),\n",
       " Card(rank=6, suit='Hearts'),\n",
       " Card(rank=7, suit='Hearts'),\n",
       " Card(rank=8, suit='Hearts'),\n",
       " Card(rank=9, suit='Hearts'),\n",
       " Card(rank=10, suit='Hearts'),\n",
       " Card(rank='J', suit='Hearts'),\n",
       " Card(rank='Q', suit='Hearts'),\n",
       " Card(rank='K', suit='Hearts'),\n",
       " Card(rank='A', suit='Hearts'),\n",
       " Card(rank=2, suit='Diamonds'),\n",
       " Card(rank=3, suit='Diamonds'),\n",
       " Card(rank=4, suit='Diamonds'),\n",
       " Card(rank=5, suit='Diamonds'),\n",
       " Card(rank=6, suit='Diamonds'),\n",
       " Card(rank=7, suit='Diamonds'),\n",
       " Card(rank=8, suit='Diamonds'),\n",
       " Card(rank=9, suit='Diamonds'),\n",
       " Card(rank=10, suit='Diamonds'),\n",
       " Card(rank='J', suit='Diamonds'),\n",
       " Card(rank='Q', suit='Diamonds'),\n",
       " Card(rank='K', suit='Diamonds'),\n",
       " Card(rank='A', suit='Diamonds'),\n",
       " Card(rank=2, suit='Clubs'),\n",
       " Card(rank=3, suit='Clubs'),\n",
       " Card(rank=4, suit='Clubs'),\n",
       " Card(rank=5, suit='Clubs'),\n",
       " Card(rank=6, suit='Clubs'),\n",
       " Card(rank=7, suit='Clubs'),\n",
       " Card(rank=8, suit='Clubs'),\n",
       " Card(rank=9, suit='Clubs'),\n",
       " Card(rank=10, suit='Clubs'),\n",
       " Card(rank='J', suit='Clubs'),\n",
       " Card(rank='Q', suit='Clubs'),\n",
       " Card(rank='K', suit='Clubs'),\n",
       " Card(rank='A', suit='Clubs')]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[card for card in deck]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One thing we don't have here is the support for `reversed`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'CardDeck' object is not reversible",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-10-6c2c7c03e9d1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mreversed\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mCardDeck\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'CardDeck' object is not reversible"
     ]
    }
   ],
   "source": [
    "reversed(CardDeck())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But we can add it in by implementing the `__reversed__` method and returning an iterator that iterates the deck in reverse order."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class CardDeck:\n",
    "    SUITS = ('Spades', 'Hearts', 'Diamonds', 'Clubs')\n",
    "    RANKS = tuple(range(2, 11)) + tuple('JQKA')\n",
    "        \n",
    "    def __iter__(self):\n",
    "        return CardDeck.card_gen()\n",
    "    \n",
    "    def __reversed__(self):\n",
    "        return CardDeck.reversed_card_gen()\n",
    "    \n",
    "    @staticmethod\n",
    "    def card_gen():\n",
    "        for suit in CardDeck.SUITS:\n",
    "            for rank in CardDeck.RANKS:\n",
    "                card = Card(rank, suit)\n",
    "                yield card\n",
    "        \n",
    "    @staticmethod\n",
    "    def reversed_card_gen():\n",
    "        for suit in reversed(CardDeck.SUITS):\n",
    "            for rank in reversed(CardDeck.RANKS):\n",
    "                card = Card(rank, suit)\n",
    "                yield card"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rev = reversed(CardDeck())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Card(rank='A', suit='Clubs'),\n",
       " Card(rank='K', suit='Clubs'),\n",
       " Card(rank='Q', suit='Clubs'),\n",
       " Card(rank='J', suit='Clubs'),\n",
       " Card(rank=10, suit='Clubs'),\n",
       " Card(rank=9, suit='Clubs'),\n",
       " Card(rank=8, suit='Clubs'),\n",
       " Card(rank=7, suit='Clubs'),\n",
       " Card(rank=6, suit='Clubs'),\n",
       " Card(rank=5, suit='Clubs'),\n",
       " Card(rank=4, suit='Clubs'),\n",
       " Card(rank=3, suit='Clubs'),\n",
       " Card(rank=2, suit='Clubs'),\n",
       " Card(rank='A', suit='Diamonds'),\n",
       " Card(rank='K', suit='Diamonds'),\n",
       " Card(rank='Q', suit='Diamonds'),\n",
       " Card(rank='J', suit='Diamonds'),\n",
       " Card(rank=10, suit='Diamonds'),\n",
       " Card(rank=9, suit='Diamonds'),\n",
       " Card(rank=8, suit='Diamonds'),\n",
       " Card(rank=7, suit='Diamonds'),\n",
       " Card(rank=6, suit='Diamonds'),\n",
       " Card(rank=5, suit='Diamonds'),\n",
       " Card(rank=4, suit='Diamonds'),\n",
       " Card(rank=3, suit='Diamonds'),\n",
       " Card(rank=2, suit='Diamonds'),\n",
       " Card(rank='A', suit='Hearts'),\n",
       " Card(rank='K', suit='Hearts'),\n",
       " Card(rank='Q', suit='Hearts'),\n",
       " Card(rank='J', suit='Hearts'),\n",
       " Card(rank=10, suit='Hearts'),\n",
       " Card(rank=9, suit='Hearts'),\n",
       " Card(rank=8, suit='Hearts'),\n",
       " Card(rank=7, suit='Hearts'),\n",
       " Card(rank=6, suit='Hearts'),\n",
       " Card(rank=5, suit='Hearts'),\n",
       " Card(rank=4, suit='Hearts'),\n",
       " Card(rank=3, suit='Hearts'),\n",
       " Card(rank=2, suit='Hearts'),\n",
       " Card(rank='A', suit='Spades'),\n",
       " Card(rank='K', suit='Spades'),\n",
       " Card(rank='Q', suit='Spades'),\n",
       " Card(rank='J', suit='Spades'),\n",
       " Card(rank=10, suit='Spades'),\n",
       " Card(rank=9, suit='Spades'),\n",
       " Card(rank=8, suit='Spades'),\n",
       " Card(rank=7, suit='Spades'),\n",
       " Card(rank=6, suit='Spades'),\n",
       " Card(rank=5, suit='Spades'),\n",
       " Card(rank=4, suit='Spades'),\n",
       " Card(rank=3, suit='Spades'),\n",
       " Card(rank=2, suit='Spades')]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[card for card in rev]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
